package cn.acheng1314.springboot2.service.impl

import cn.acheng1314.exceptions.NotFoundException
import cn.acheng1314.springboot2.dto.HouseNewsDTO
import cn.acheng1314.springboot2.entity.HouseNews
import cn.acheng1314.springboot2.mapper.HouseNewsMapper
import cn.acheng1314.springboot2.service.IHouseNewsService
import cn.acheng1314.springboot2.service.IHousePropertyService
import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

/**
 * 房源服务
 * @author cheng
 * @date 2019年12月15日 23:51:40
 */
@Service
open class HouseNewsServiceImpl : ServiceImpl<HouseNewsMapper, HouseNews>(), IHouseNewsService {

    @Autowired
    private lateinit var housePropertyService: IHousePropertyService

    override fun addHouseNews(dto: HouseNewsDTO): HouseNews {
        val news = JSONObject.parseObject(JSONObject.toJSONString(dto), HouseNews::class.java)
        housePropertyService.getById(news.houseId) ?: throw NotFoundException("未找到房源信息！")
        news.id = UUID.randomUUID().toString()
        save(news)
        return news
    }


}